//
//  M1PhotoAlbumLayout.m
//  NewMarryU
//
//  Created by Cedar on 2018/7/16.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1PhotoAlbumLayout.h"

typedef CGFloat(^HeightBlock)(NSIndexPath *indexPath , CGFloat width);

@interface M1PhotoAlbumLayout ()

/**
 *  存放每列高度字典
 */
@property (nonatomic, strong) NSMutableDictionary *m1_dicOfheight;
/**
 *  存放所有item的attrubutes属性
 */
@property (nonatomic, strong) NSMutableArray *m1_array;
/**
 *  计算每个item高度的block，必须实现
 */
@property (nonatomic, copy) HeightBlock m1_block;

@end

@implementation M1PhotoAlbumLayout

- (instancetype)init {
    
    self = [super init];
    if (self) {
        
        //对默认属性进行设置
        //默认行数 3行
        //默认行间距 10.0f
        //默认列间距 10.0f
        //默认内边距 top:10 left:10 bottom:10 right:10
        
        self.m1_lineNumber = 3;
        self.m1_rowSpacing = 7.0f;
        self.m1_lineSpacing = 7.0f;
        self.m1_sectionInset = UIEdgeInsetsMake(7, 7, 7, 7);
        _m1_dicOfheight = [NSMutableDictionary dictionary];
        _m1_array = [NSMutableArray array];
    }
    return self;
}
/**
 *  准备好布局时调用
 */
- (void)prepareLayout {
    
    [super prepareLayout];
    
    [self.m1_array removeAllObjects];
    
    NSInteger count = [self.collectionView numberOfItemsInSection:0];
    //初始化好每列的高度
    for (NSInteger i = 0; i < self.m1_lineNumber ; i++) {
        
        [_m1_dicOfheight setObject:@(self.m1_sectionInset.top) forKey:[NSString stringWithFormat:@"%ld",i]];
    }
    //得到每个item的属性值进行存储
    for (NSInteger i = 0 ; i < count; i ++) {
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [_m1_array addObject:[self layoutAttributesForItemAtIndexPath:indexPath]];
    }
}
/**
 *  设置可滚动区域范围
 *
 *  @return 可滚动区域的size
 */
- (CGSize)collectionViewContentSize {
    
    NSLog(@"collectionViewContentSize");
    
    __block NSString *maxHeightline = @"0";
    [_m1_dicOfheight enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSNumber *obj, BOOL *stop) {
        
        if ([self.m1_dicOfheight[maxHeightline] floatValue] < [obj floatValue] ) {
            maxHeightline = key;
        }
    }];
    return CGSizeMake(self.collectionView.bounds.size.width, [_m1_dicOfheight[maxHeightline] floatValue] + self.m1_sectionInset.bottom);
    
}
/**
 *
 *  @return 返回视图框内item的属性，可以直接返回所有item属性
 */
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect {return _m1_array;
    
}
/**
 *  计算indexPath下item的属性的方法
 *
 *  @return item的属性
 */
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
    //通过indexPath创建一个item属性attr
    UICollectionViewLayoutAttributes *attr = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    
    CGFloat itemW;
    if (indexPath.item == 0) {
        itemW = (SCREENWIDTH - 28) / 3 * 2 + 6;
    }else{
        itemW = (SCREENWIDTH - 28) / 3;
    }
    CGFloat itemH = 0.0;
    //计算item高
    if (self.m1_block != nil) {
        
        itemH = self.m1_block(indexPath, itemW);
    } else {
        
        NSAssert(itemH != 0,@"Please implement computeIndexCellHeightWithWidthBlock Method");
    }
    //计算item的frame
    CGRect frame;
    frame.size = CGSizeMake(itemW, itemH);
    //循环遍历找出高度最短行
    __block NSString *lineMinHeight = @"0";
    [_m1_dicOfheight enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSNumber *obj, BOOL *stop) {
        
        if ([self.m1_dicOfheight[lineMinHeight] floatValue] > [obj floatValue]) {
            
            lineMinHeight = key;
        }
    }];
    int line;
    if (indexPath.item == 2){
        line = 1;
        lineMinHeight = @"1";
    }else{
        line = [lineMinHeight intValue];
    }
    //找出最短行后，计算item位置
    frame.origin = CGPointMake(self.m1_sectionInset.left + line * (itemW + self.m1_lineSpacing), [_m1_dicOfheight[lineMinHeight] floatValue]);
    _m1_dicOfheight[lineMinHeight] = @(frame.size.height + self.m1_rowSpacing + [_m1_dicOfheight[lineMinHeight] floatValue]);
    attr.frame = frame;
    return attr;
}
/**
 *  设置计算高度block方法
 *
 *  @param block 计算item高度的block
 */
- (void)m1_computeIndexCellHeightWithWidthBlock:(CGFloat (^)(NSIndexPath *, CGFloat))block {
    
    if (self.m1_block != block) {
        
        self.m1_block = block;
    }
}

@end
